﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Commonlib;
namespace AlbonWeb.Manager.OrderManager
{
    public partial class ShipRecord : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["limit"] = " where 1=1";
                bindShip();
                
            }
        }

        //绑定发货记录
        protected void bindShip()
        {
            if (trackNo.Text.Trim() != "")
            {
                ViewState["limit"] += " and shipNo like '%"+trackNo.Text.Trim()+"%'";
            }
            //查询包含该订单的包裹，并将包裹内的对应项突出显示
            if (Request["orderid"] != null)
            {
                ViewState["limit"] += " and shipNo in (select shipNo from ShipMent where orderItemID in (select ID from OrderItem where orderID="+Request["orderid"].ToString()+"))";
            }
            string cmdstr = "select count(*) from ShipMentView " + ViewState["limit"].ToString();
            AspNetPager1.RecordCount = int.Parse(SqlHelper.ExecuteScalar(SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null).ToString());
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@tablename","ShipMentView"),
                new SqlParameter("@startIndex",AspNetPager1.StartRecordIndex),
                new  SqlParameter("@endIndex",AspNetPager1.EndRecordIndex),
                new SqlParameter("@limit",ViewState["limit"].ToString()),
                new SqlParameter("@orderby","createDate desc")
            };
            SqlHelper.BindRepeater(repShipMent, SqlHelper.SqlConnectionString, CommandType.StoredProcedure, "netpager", parameters);
        }
        //绑定 包裹内的订单项数据
        protected void repShipMent_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            string shipNo = ((HiddenField)e.Item.FindControl("hfShipNo")).Value;
            string cmdstr = "select * from ItemShipView where ID in (select orderItemID from ShipMent where shipNo='"+shipNo
                +"')";
            Repeater repItem = (Repeater)e.Item.FindControl("repItem");
            SqlHelper.BindRepeater(repItem,SqlHelper.SqlConnectionString,CommandType.Text,cmdstr,null);
        }
        //换页
        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            bindShip();
        }

        //搜索
        protected void btnSearch_Click(object sender, ImageClickEventArgs e)
        {
            ViewState["limit"] = "where 1=1";
            if (trackNo.Text.Trim() != "")
            {
                ViewState["limit"] += " and shipNo like '%" + trackNo.Text.Trim() + "%'";
            }
            bindShip();
        }
    }
}